<template>
  <!--课程购买-->
  <div>
    <headers></headers>
    <div class="padding88">
      <div class="divider">
        <div class="auto">
          <div v-if="coursetype == 0">
          <router-link to="/">首页</router-link> <span>></span>推荐课程列表
          </div>
          <div v-else>
            <a @click.stop.prevent="allCourse">全部课程</a><a @click.stop.prevent="coursetypeChange" v-if="coursetypeFirst != ''">>{{coursetypeFirst.courseTypeName}}</a><a v-if="coursetypeSecond != ''">>{{coursetypeSecond.courseTypeName}}</a>
          </div>
        </div>
      </div>
    </div>
    <div class="navs">
      <div class="auto">
        <div class="title">
          课程
          <ul class="pull-right">
            <li :class="{active:0===nowIndex}"  @click="tab(0)"><router-link to="##" class="font14">全部</router-link></li>
            <li :class="{active:1===nowIndex}"  @click="tab(1)"><router-link to="##" class="font14">免费</router-link></li>
            <li :class="{active:2===nowIndex}"  @click="tab(2)"><router-link to="##" class="font14">VIP</router-link></li>
            <li :class="{active:3===nowIndex}"  @click="tab(3)"><router-link to="##" class="font14">价格升序</router-link></li>
            <li :class="{active:4===nowIndex}"  @click="tab(4)"><router-link to="##" class="font14">价格降序</router-link></li>
          </ul>
        </div>
      </div>
    </div>
      <div class="auto padding250">
        <span v-if="courses.length !== undefined && courses.length > 0">
        <course v-bind:courses="courses" v-bind:online="1" v-bind:isVip = "isVip"></course>
        <div class="pageout"><Page :total="total" :current="pageNumber" :page-size="size" @on-change="changePage"/></div>
        </span>
        <span v-else>
          <nodata tip="暂时还没有课程哦~"></nodata>
        </span>
      </div>
    <Spin fix v-show="isSpinShow" >
      <div class="loader">
        <Icon type="ios-loading" size=26 class="demo-spin-icon-load"></Icon>
        <div class="font14">加载中...</div>
      </div>
    </Spin>
    <footers></footers>
    <loginmodal  ref="login1"></loginmodal>
  </div>
</template>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="less" rel="stylesheet/less">
  /* 旋转效果 */
  .demo-spin-icon-load{
    animation: ani-demo-spin 1s linear infinite;
  }
  @keyframes ani-demo-spin {
    from { transform: rotate(0deg);}
    50% { transform: rotate(180deg);}
    to { transform: rotate(360deg);}
  }
  /* 假如内容过长，一屏放不下，滚动条下拉覆盖不全 */
  .ivu-spin-fix {
    position: fixed;
  }

  .divider{
    height: 60px;
    background-color: #2F261E;
    font-size: 14px;
    line-height: 60px;
    text-align: left;
    color: #fff;
    span{
      margin: 0 10px;
    }
    a{
      color: #fff;
      &:hover{
        color: #9C7649;
        font-weight: 700;
      }
    }
  }
  .navs{
    background-color: #fff;
  }
  .title{
    font-size: 20px;
    color: #000;
    padding: 15px 10px 15px 0;
    text-align: left;
    li{
      display: inline-block;
      margin-right: 30px;
      a{
        padding: 5px 5px 15px;
        font-size: 14px;
        color: #333;
      }
    }
    li.active {
      a{
        font-weight: 700;
        color: #957960;
        border-bottom: 3px solid #957960;
      }
    }
    li:last-child{
      margin-right: 0;
    }
  }
</style>
<script type="text/ecmascript-6">
  import course from '../template/course'
  import headers from '../template/header'
  import footers from '../template/footer'
  import nodata from '../template/null'
  import loginmodal from '../template/loginmodal'
  import {courseListWeb,courseListFirstWeb,recommendListWeb,courseAllWeb} from '../../http'
  export default {
    inject :['reload'],
    name: 'list',
    props: {},
    data() {
      return {
        isSpinShow:false,
        nowIndex:0,           //视频类别选择
        courses:'',           //视频内容
        total:0,              //总数
        size:16,              //每页个数
        pageNumber:1,         //页面
        sort:'S0',            //排序规则
        coursetypeId:this.$route.query.coursetypeId, //课程类别Id
        coursetype:this.$route.query.coursetype,     //课程类别
        coursetypeFirst:'',   //一级课程类别
        coursetypeSecond:'',  //二级课程类别
        isVip:0,
      };
    },
    methods: {
      allCourse(){
        this.$router.push({
          name:'courselist',
          query:{
            coursetype:-1,
          }
        });
        this.reload();
      },
      //课程请求
      qingqiu(){
        if(this.isSpinShow === false) {
          this.isSpinShow = true;
          if (this.coursetype == -1) {
            //全部课程
            courseAllWeb({sortOrder: this.sortOrder, pageNumber: this.pageNumber,pageSize:this.size}).then(res => {
              this.isSpinShow = false;
              if (res.code === 200) {
                this.courses = res.data.lists.rows;
                this.total = res.data.lists.total;
                this.coursetypeFirst = res.data.coursetypeFirst;
                this.coursetypeSecond = res.data.coursetypeSecond;
                this.isVip = res.data.isVip;
              } else {
                this.$Notice.warning({
                  title: '系统错误',
                  desc: '请刷新页面',
                  duration: 3,
                });
              }
            }).catch()
          } else if (this.coursetype == 0) {
            //推荐课程
            recommendListWeb({sortOrder: this.sortOrder, pageNumber: this.pageNumber,pageSize:this.size}).then(res => {
              this.isSpinShow = false;
              if (res.code === 200) {
                this.courses = res.data.lists.rows;
                this.total = res.data.lists.total;
                this.coursetypeFirst = res.data.coursetypeFirst;
                this.coursetypeSecond = res.data.coursetypeSecond;
                this.isVip = res.data.isVip;
              } else {
                this.$Notice.warning({
                  title: '系统错误',
                  desc: '请刷新页面',
                  duration: 3,
                });
              }
            }).catch()
          } else if (this.coursetype == 1) {
            //一级类别课程
            courseListFirstWeb({
              coursetypeId: this.coursetypeId,
              sortOrder: this.sortOrder,
              pageNumber: this.pageNumber,
              pageSize:this.size
            }).then(res => {
              this.isSpinShow = false;
              if (res.code === 200) {
                this.courses = res.data.lists.rows;
                this.total = res.data.lists.total;
                this.coursetypeFirst = res.data.coursetypeFirst;
                this.coursetypeSecond = res.data.coursetypeSecond;
                this.isVip = res.data.isVip;
              } else {
                this.$Notice.warning({
                  title: '系统错误',
                  desc: '请刷新页面',
                  duration: 3,
                });
              }
            }).catch()
          } else {
            //二级类别课程
            courseListWeb({
              coursetypeId: this.coursetypeId,
              sortOrder: this.sortOrder,
              pageNumber: this.pageNumber,
              pageSize:this.size
            }).then(res => {
              this.isSpinShow = false;
              if (res.code === 200) {
                this.courses = res.data.lists.rows;
                this.total = res.data.lists.total;
                this.coursetypeFirst = res.data.coursetypeFirst;
                this.coursetypeSecond = res.data.coursetypeSecond;
                this.coursetypeSecond = res.data.coursetypeSecond;
                this.isVip = res.data.isVip;
              } else {
                this.$Notice.warning({
                  title: '系统错误',
                  desc: '请刷新页面',
                  duration: 3,
                });
              }
            }).catch()
          }
        }
      },
      //课程类别切换
      coursetypeChange(){
        this.$router.push({
          name:'courselist',
          query:{
            coursetype:1,
            coursetypeId:this.coursetypeFirst.id,
          }
        });
        this.reload();
      },
      //课程排序切换
      tab(index){
        this.nowIndex = index;
        this.sortOrder = 'S' + index;
        this.pageNumber = 1;
        this.qingqiu();
      },
      //页码切换
      changePage(pageNumber){
        this.pageNumber = pageNumber;
        this.qingqiu();
      }
    },
    created() {
      //复合页面，根据不同情况请求数据
      this.qingqiu();
    },
    components: {
      headers,
      footers,
      course,
      nodata,
      loginmodal
    }
  }
</script>
